package com.zeroup.matrix.utile;



import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;

import androidx.core.content.FileProvider;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.bytedance.sdk.open.aweme.base.ImageObject;
import com.bytedance.sdk.open.aweme.base.MediaContent;
import com.bytedance.sdk.open.aweme.base.MixObject;
import com.bytedance.sdk.open.aweme.base.ShareParam;
import com.bytedance.sdk.open.aweme.base.StickersObject;
import com.bytedance.sdk.open.aweme.base.TitleObject;
import com.bytedance.sdk.open.aweme.base.VideoObject;
import com.bytedance.sdk.open.aweme.base.openentity.HashtagSticker;
import com.bytedance.sdk.open.aweme.common.constants.ParamKeyConstants;
import com.bytedance.sdk.open.aweme.share.Share;
import com.bytedance.sdk.open.douyin.DouYinOpenApiFactory;
import com.bytedance.sdk.open.douyin.api.DouYinOpenApi;
import com.zeroup.matrix.MainActivity;
import com.zeroup.matrix.VediosActivity;
import com.zeroup.matrix.bean.MGanVideo;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

public class DouyinUtiles {

    public static void push(Activity activity, MGanVideo video,String localFilePath){
        Context context = activity.getApplicationContext();
        Share.Request request = new Share.Request();
        request.mState = video!=null?video.getGid():"111"; //建议传入OpenAPI中申请的ShareID，分享结果会通过 Webhooks 进行回调。更多信息，请参见查询视频分享结果及数据。
        // request.callerLocalEntry =  DouYinEntryActivity.class.getCanonicalName(); //自定义回调Activity

//        File file = new File(localFilePath);
//        Uri uri =Uri.fromFile(file);
        ArrayList<String> mUri = new ArrayList<>();
        // 初始化资源路径，路径请使用FileProvider路径
        File file = new File(localFilePath);
        //此处需要申明FileProvider，详情参考 Android 分享支持 FileProvider 的方式
        Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileProvide", file);
        //分别给抖音、抖音极速版、抖音火山版授权
        context.grantUriPermission(ParamKeyConstants.DOUYIN_PACKAGE_NAME, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
//        context.grantUriPermission(ParamKeyConstants.DOUYIN_LITE_PACKAGE_NAME, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
//        context.grantUriPermission(ParamKeyConstants.DOUYIN_HOTSOON_PACKAGE_NAME, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        mUri.add(uri.toString());
        LogUtils.dTag("DouyinUtiles===>",uri.toString());

        // 分享图片
//        ImageObject imageObject = new ImageObject();
//        imageObject.mImagePaths = mUri;
//        MediaContent mediaContent = new MediaContent();
//        mediaContent.mMediaObject = imageObject;
//        request.mMediaContent = mediaContent;


        //分享视频
        VideoObject videoObject = new VideoObject();
        videoObject.mVideoPaths = mUri;
        MediaContent videoContent = new MediaContent();
        videoContent.mMediaObject = videoObject;
        request.mMediaContent = videoContent;

        request.callerLocalEntry = VediosActivity.class.getName();

        ShareParam shareParam = new ShareParam();
        request.shareParam = shareParam;

        TitleObject titleObject = new TitleObject();
        shareParam.titleObject = titleObject;
        titleObject.shortTitle  = video.getSubTitle();   // 抖音30.0.0版本开始支持该字段
        titleObject.title = video.getPushCopywriting();

//        StickersObject stickersObject = new StickersObject();

        if(!StringUtils.isEmpty(video.getTags())){
            request.mHashTagList = new ArrayList<>(Arrays.asList(video.getTags().split("#")));
        }

//        HashtagSticker hashtagSticker = new HashtagSticker(); //话题贴纸
//        hashtagSticker.name = video.getTags();
//        stickersObject.addSticker(hashtagSticker);
//        shareParam.stickersObject = stickersObject;
        shareParam.poiId = video.getPoiId();


        DouYinOpenApi douyinOpenApi = DouYinOpenApiFactory.create(activity);

        // 分享混合内容（要求SDK版本至少为0.1.7.0，抖音版本至少为17.4.0）
//        if (douyinOpenApi.isAppSupportMixShare()) {
//            MixObject mixObject = new MixObject();
//            mixObject.mMediaPaths = mUri;
//            MediaContent mixContent = new MediaContent();
//            mixContent.mMediaObject = mixObject;
//            request.mMediaContent = mixContent;
//        }


        //如果预期直接拉起发布页，可以判断抖音是否支持。
        // 目前直接到发布页仅支持单个视频
        if(douyinOpenApi.isAppSupportShareToPublish()) {
            request.shareToPublish = true;
        }
        // 调起分享
        douyinOpenApi.share(request);


    }
}
